package com.heaboy.feedback.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.heaboy.databind.binding.Binder;
import com.heaboy.feedback.constant.EnumLessonStatus;
import com.heaboy.feedback.domain.CourseClazz;
import com.heaboy.feedback.domain.vo.CourseClazzVO;
import com.heaboy.feedback.domain.vo.LessonVO;
import com.heaboy.feedback.domain.vo.StudentInfoVO;
import com.heaboy.feedback.service.ICourseClazzService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.Seq;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.heaboy.feedback.mapper.LessonMapper;
import com.heaboy.feedback.domain.Lesson;
import com.heaboy.feedback.service.ILessonService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
/**
 * 课Service业务层处理
 * 
 * @author heaboy
 * @date 2024-02-01
 */
@Service
public class LessonServiceImpl extends ServiceImpl<LessonMapper, Lesson> implements ILessonService
{
    @Autowired
    private LessonMapper lessonMapper;

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ICourseClazzService courseClazzService;

    /**
     * 查询课
     * 
     * @param id 课主键
     * @return 课
     */
    @Override
    public Lesson selectLessonById(Long id)
    {
        return lessonMapper.selectLessonById(id);
    }

    /**
     * 查询课列表
     * 
     * @param lesson 课
     * @return 课
     */
    @Override
    public List<Lesson> selectLessonList(Lesson lesson)
    {
        return lessonMapper.selectLessonList(lesson);
    }

    /**
     * 新增课
     * 
     * @param lesson 课
     * @return 结果
     */
    @Override
    public int insertLesson(Lesson lesson)
    {
        lesson.setCreateTime(DateUtils.getNowDate());
        return lessonMapper.insertLesson(lesson);
    }

    /**
     * 修改课
     * 
     * @param lesson 课
     * @return 结果
     */
    @Override
    public int updateLesson(Lesson lesson)
    {
        lesson.setUpdateTime(DateUtils.getNowDate());
        return lessonMapper.updateLesson(lesson);
    }

    /**
     * 批量删除课
     * 
     * @param ids 需要删除的课主键
     * @return 结果
     */
    @Override
    public int deleteLessonByIds(Long[] ids)
    {
        return lessonMapper.deleteLessonByIds(ids);
    }

    /**
     * 删除课信息
     * 
     * @param id 课主键
     * @return 结果
     */
    @Override
    public int deleteLessonById(Long id)
    {
        return lessonMapper.deleteLessonById(id);
    }

    @Override
    public Lesson start(Long courseClazzId) {
        CourseClazz courseClazz = courseClazzService.getById(courseClazzId);
        CourseClazzVO courseClazzVO = Binder.convertAndBindRelations(courseClazz, CourseClazzVO.class);
        Lesson lesson = new Lesson();
        lesson.setCourseClazzId(courseClazzId);
        lesson.setTeacherId(SecurityUtils.getUserId());
        lesson.setName(courseClazzVO.getCourseName()+courseClazzVO.getClazzName()+ Seq.getId());
        lesson.setCreateTime(DateUtils.getNowDate());
        lesson.setCreateBy(SecurityUtils.getUsername());
        lesson.setStatus(EnumLessonStatus.START.getCode());
        int i = baseMapper.insertLesson(lesson);
        return lesson;
    }

    @Override
    public Lesson goLesson(Long id) {
        return baseMapper.selectLessonById(id);
    }

    @Override
    public LessonVO lessonInfo(Long id) {
        List<SysUser> studentList = sysUserService.selectUserByLessonId(id);
        List<StudentInfoVO> studentInfoVOS = new ArrayList<StudentInfoVO>(studentList.size());
        for (SysUser sysUser : studentList) {
            studentInfoVOS.add(new StudentInfoVO(sysUser));
        }
        //todo: 查询是否在签到状态,查询最后一次提问信息 将 showState进行修改
        //todo: 如果 没有提问 那么恢复 签到状态 如果有提问 则 恢复最后一次提问回答状态
        LessonVO lessonVO = new LessonVO();
        lessonVO.setStudentList(studentInfoVOS);
        return lessonVO;
    }
}
